package com.liyuxiang.graph.service;

import com.liyuxiang.feign.client.MedicalCaseClient;
import com.liyuxiang.feign.entity.MedicalCaseDto;
import com.liyuxiang.feign.vm.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: liyuxiang
 * @Date: 2025/8/23 16:04
 * @Description: 医疗病例服务，用于AI工具调用
 */
@Slf4j
@Service
public class MedicalCaseService {

    @Autowired
    private MedicalCaseClient medicalCaseClient;

    /**
     * AI工具函数：添加医疗病例
     * 在推荐科室后自动调用，保存患者的问诊记录
     */
    @Tool(description = "添加医疗病例记录，用于保存患者问诊信息", name = "addMedicalCase")
    public String addMedicalCase(@ToolParam(description = "患者ID") Long patientId,
                                @ToolParam(description = "推荐的科室ID") Long deptId,
                                @ToolParam(description = "患者主要症状描述") String chiefComplaint) {
        try {
            log.info("AI工具调用：开始添加医疗病例 - 患者ID: {}, 科室ID: {}", patientId, deptId);
            
            MedicalCaseDto dto = new MedicalCaseDto();
            dto.setPatientId(patientId);
            dto.setDeptId(deptId);
            dto.setChiefComplaint(chiefComplaint);
            
            ResponseResult<Void> result = medicalCaseClient.add(dto);
            
            if (result != null && result.getCode() == 200) {
                log.info("AI工具调用：医疗病例添加成功 - 患者ID: {}", patientId);
                return "医疗病例已成功添加到系统中";
            } else {
                log.error("AI工具调用：医疗病例添加失败 - 响应: {}", result);
                return "医疗病例添加失败，请稍后重试";
            }
        } catch (Exception e) {
            log.error("AI工具调用：医疗病例添加异常 - 患者ID: {}, 错误: {}", 
                     patientId, e.getMessage(), e);
            return "医疗病例添加出现异常：" + e.getMessage();
        }
    }
}
